//
//  AppTool.m
//  AddressList
//
//  Created by dllo on 15/12/10.
//  Copyright © 2015年 尹凯. All rights reserved.
//

#import "AppTool.h"
#import "ChineseToPinyin.h"

@implementation AppTool

+ (instancetype)shareAppTool{
    static AppTool *appTool = nil;
    if (appTool == nil) {
        appTool = [[AppTool alloc] init];
    }
    return appTool;
}

- (void)insertStudentName:(NSString *)name {
    //获取表结构
    //参数1.实体名  表名
    //参数2.数据管理员
    
    /*
     NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.managerObjectContext];
     
     Student *aStudent = [[Student alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:self.managerObjectContext];
     */
    
    Student *aStudent = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:[[CoreDataManager shareManager] managedObjectContext]];
    
    aStudent.name = name;
//    aStudent.gender = student.gender;
//    aStudent.telNumber = student.telNumber;
    
    //保存
    [[CoreDataManager shareManager] saveContext];
    
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSLog(@"🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶%@", document);
    
    NSLog(@"我讨厌肥贼");
}

- (void)deleteStudent:(Student *)student {
    [[[CoreDataManager shareManager] managedObjectContext] deleteObject:student];
    [[CoreDataManager shareManager] saveContext];
}

//- (void)changeStudent:(Student *)student {
//    Student *student = self.resultArr.lastObject;
//    
//    student.name = @"李昞辰";
//    student.gender = @"女";
//    [[CoreDataManager shareManager] saveContext];
//}

- (NSMutableDictionary *)selectStudent {
    
    //查询请求
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    
    //获取要查询的实体的描述
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:[[CoreDataManager shareManager] managedObjectContext]];
    
    //对fetchRequest设置查询的实体
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch
    
//    //谓词
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@", @"马强"];
//    [fetchRequest setPredicate:predicate];
    // Specify how the fetched objects should be sorted
    
    //排序r
    //第一个参数  按哪个字段排序
    //第二个参数  是否升序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    //从数据库中搜索满足上面条件的元素
    NSArray *fetchedObjects = [[[CoreDataManager shareManager] managedObjectContext] executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"%@", error);
        return nil;
    }
    
    NSArray *array = [NSMutableArray arrayWithArray:fetchedObjects];
    
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    
    for (Student *studnet in array) {
        
        NSString *pinyin = [ChineseToPinyin pinyinFromChiniseString:studnet.name];
        NSString *firstStr = [pinyin substringToIndex:1];
        if (![dictionary.allKeys containsObject:firstStr]) {
            NSMutableArray *tempArray = [NSMutableArray array];
            [tempArray addObject:studnet];
            [dictionary setValue:tempArray forKey:firstStr];
        } else{
            NSMutableArray *tempArray = [dictionary objectForKey:firstStr];
            [tempArray addObject:studnet];
        }
        
    }
    return dictionary;
}

@end
